/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NMTManager.java
 *
 * Created on Jan 8, 2011, 5:14:43 PM
 */

package yayman;

import javax.swing.DefaultListModel;
import java.util.logging.*;
import org.jdesktop.application.Action;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

/**
 *
 * @author Dan
 */
public class NMTManager extends javax.swing.JFrame {

    private Logger logger = Logger.getLogger("yayman");
    private NetworkedMediaTank currentNmt;
    private DefaultListModel nmtModel;
    private PopAPI popAPI;


    /** Creates new form NMTManager */
    public NMTManager() {
        initComponents();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(yayman.YAYManApp.class).getContext().getResourceMap(NMTManager.class);
        javax.swing.ImageIcon frameIcon = resourceMap.getImageIcon("nmtFrameIcon");
        this.setIconImage(frameIcon.getImage());

        PopAPI.searchNmts();

        nmtModel = new DefaultListModel();
        PopAPI.syncNmtsWith(nmtModel);
        lstNmt.setModel(nmtModel);
        popAPI = null;
    }

    public NMTManager(String nmtName) {
        this();

        for(int i = 0; i < nmtModel.getSize(); i++) {
            NetworkedMediaTank nmt = (NetworkedMediaTank)nmtModel.get(i);
            if (nmt.getName().equals(nmtName)) {
                lstNmt.setSelectedIndex(i);
            }
        }

        if (lstNmt.getSelectedIndex() == -1 && nmtModel.getSize() > 0) lstNmt.setSelectedIndex(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstShares = new javax.swing.JList();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtShareHost = new javax.swing.JTextField();
        txtShareName = new javax.swing.JTextField();
        txtShareUser = new javax.swing.JTextField();
        btnAddShare = new javax.swing.JButton();
        txtSharePass = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        cmbProtocol = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        txtShareFolder = new javax.swing.JTextField();
        lblNmtName = new javax.swing.JLabel();
        lblNmtAddress = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstNmt = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(yayman.YAYManApp.class).getContext().getResourceMap(NMTManager.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jSplitPane1.setDividerLocation(125);
        jSplitPane1.setName("jSplitPane1"); // NOI18N
        jSplitPane1.setPreferredSize(new java.awt.Dimension(500, 201));

        jPanel1.setName("jPanel1"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        lstShares.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstShares.setName("lstShares"); // NOI18N
        jScrollPane2.setViewportView(lstShares);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txtShareHost.setText(resourceMap.getString("txtShareHost.text")); // NOI18N
        txtShareHost.setName("txtShareHost"); // NOI18N

        txtShareName.setText(resourceMap.getString("txtShareName.text")); // NOI18N
        txtShareName.setName("txtShareName"); // NOI18N

        txtShareUser.setText(resourceMap.getString("txtShareUser.text")); // NOI18N
        txtShareUser.setName("txtShareUser"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(yayman.YAYManApp.class).getContext().getActionMap(NMTManager.class, this);
        btnAddShare.setAction(actionMap.get("addShare")); // NOI18N
        btnAddShare.setText(resourceMap.getString("btnAddShare.text")); // NOI18N
        btnAddShare.setName("btnAddShare"); // NOI18N

        txtSharePass.setText(resourceMap.getString("txtSharePass.text")); // NOI18N
        txtSharePass.setName("txtSharePass"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        cmbProtocol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SAMBA", "NFS", "NFS TCP" }));
        cmbProtocol.setName("cmbProtocol"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        txtShareFolder.setText(resourceMap.getString("txtShareFolder.text")); // NOI18N
        txtShareFolder.setName("txtShareFolder"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel6)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(13, 13, 13)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtSharePass, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                            .addComponent(txtShareUser, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                            .addComponent(txtShareFolder, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                            .addComponent(txtShareHost, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                            .addComponent(cmbProtocol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtShareName, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                            .addComponent(btnAddShare))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtShareName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(cmbProtocol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtShareHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(txtShareFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtShareUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtSharePass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAddShare))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        lblNmtName.setFont(resourceMap.getFont("lblNmtName.font")); // NOI18N
        lblNmtName.setText(resourceMap.getString("lblNmtName.text")); // NOI18N
        lblNmtName.setName("lblNmtName"); // NOI18N

        lblNmtAddress.setText(resourceMap.getString("lblNmtAddress.text")); // NOI18N
        lblNmtAddress.setName("lblNmtAddress"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                    .addComponent(lblNmtName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                    .addComponent(lblNmtAddress, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNmtName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblNmtAddress)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(jPanel1);

        jPanel3.setName("jPanel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        lstNmt.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstNmt.setName("lstNmt"); // NOI18N
        lstNmt.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstNmtValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstNmt);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 124, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 329, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        jSplitPane1.setLeftComponent(jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 331, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lstNmtValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstNmtValueChanged
        if (lstNmt.getSelectedIndex() == -1) {
            lblNmtName.setText("[Name]");
            lblNmtAddress.setText("[Address]");
            currentNmt = null;
            popAPI = null;
            return;
        }

        currentNmt = (NetworkedMediaTank)lstNmt.getSelectedValue();
        lblNmtName.setText(currentNmt.getName());
        lblNmtAddress.setText(currentNmt.getAddress());
        popAPI = new PopAPI(currentNmt.getAddress());
        updateShareList();
    }

    @Action
    public void addShare() {
        if (txtShareName.getText().isEmpty() || txtShareHost.getText().isEmpty() || txtShareFolder.getText().isEmpty()) {
            return;
        }

        /*String module = "setting";
        String[] params = {"add_network_shared_folder", txtShareName.getText(), txtShareHost.getText(), txtShareUser.getText(), (txtShareUser.getText().isEmpty()) ? "" : new String(txtSharePass.getPassword()) };
        Document doc = popAPI.request(module, params);
        Element request = (Element)doc.getElementsByTagName("request").item(0);
        NodeList args = request.getChildNodes();
        for (int i = 0; i < args.getLength(); i++) {
            System.out.println(args.item(i).getNodeName()+": "+args.item(i).getTextContent());
        }
        Element returnVal = (Element)doc.getElementsByTagName("returnValue").item(0);
        System.out.println(returnVal.getTagName()+": "+returnVal.getTextContent());*/
        Document doc = popAPI.addShare(""+cmbProtocol.getSelectedIndex(), txtShareHost.getText(), txtShareFolder.getText(), txtShareName.getText(), txtShareUser.getText(), new String(txtSharePass.getPassword()));
        updateShareList();
    }//GEN-LAST:event_lstNmtValueChanged

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NMTManager().setVisible(true);
            }
        });
    }*/

    public void updateShareList() {
        DefaultListModel shareModel = new DefaultListModel();
        if (popAPI != null) {
            Document doc = popAPI.getShares();
            if (popAPI.querySuccess(doc)) {
                NodeList nl = doc.getElementsByTagName("networkShare");
                for (int i = 0; i < nl.getLength(); i++) {
                    Element shareEle = (Element)nl.item(i);
                    String shareName = shareEle.getElementsByTagName("shareName").item(0).getTextContent();
                    shareModel.addElement(shareName);
                }
            }
        }
        lstShares.setModel(shareModel);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddShare;
    private javax.swing.JComboBox cmbProtocol;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblNmtAddress;
    private javax.swing.JLabel lblNmtName;
    private javax.swing.JList lstNmt;
    private javax.swing.JList lstShares;
    private javax.swing.JTextField txtShareFolder;
    private javax.swing.JTextField txtShareHost;
    private javax.swing.JTextField txtShareName;
    private javax.swing.JPasswordField txtSharePass;
    private javax.swing.JTextField txtShareUser;
    // End of variables declaration//GEN-END:variables

}
